/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.sun.lwuit.Command;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BorderLayout;
import core.ScreenEngine;
import view.additional.CellItem;
import view.additional.ThumbList;
import view.additional.TickerRenderer;
import view.commands.BackCommand;

/**
 *
 * @author Kvest
 */
public class InfoForm  extends Form
{
    private static final int SELECT_COMMAND_ID = 1;
    
    private static final String TEXT_POSTFIX = "_text"; 
    private static final String[] ITEMS_STR = new String[]{"info1", "info2"};
    
    private ThumbList items;
    private Command select;
    
    private Label tmpLabel;
    
    private TextForm textForm;
    
    public InfoForm()
    {
        super("info");
        
        tmpLabel = new Label();
        
        init();
    }
    
     private void init()
    {
        setLayout(new BorderLayout());
        setScrollable(false);
        
        //Создаем список
        items = new ThumbList();
        items.setSelectCommandText("...");
        items.setFixedSelection(ThumbList.FIXED_NONE_CYCLIC);
        TickerRenderer render = new TickerRenderer();
        items.setRenderer(render);
        
        initItems();
        addComponent(BorderLayout.CENTER, items);
        
        //Команды
        select = new Command("select",SELECT_COMMAND_ID)
        {
            public void actionPerformed(ActionEvent evt)
            {
                selectAction();
                
                evt.consume();
            }
        };
        addCommand(select);
        
        items.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent evt) 
            {
                select.actionPerformed(evt);
            }
        });
        
        addCommand(new BackCommand());
    }
     
    private void initItems()
    {
        for (int i = 0; i < ITEMS_STR.length; ++i)
        {
            items.addItem(new CellItem(i, ITEMS_STR[i]));
        }
    }
    
    private void selectAction()
    {
        CellItem selected = (CellItem)items.getSelectedItem();
        
        if (selected != null)
        {
            showTextForm(ITEMS_STR[selected.getId()]);
        }
    }
    
    private void showTextForm(String text)
    {
        //Создаем форму
        if (textForm == null)
        {
            textForm = new TextForm();
        }
        
        //Устанавливаем текст
        textForm.setTitle(text);
        tmpLabel.setText(text + TEXT_POSTFIX);
        textForm.setText(tmpLabel.getText());
        
        //Отображаем
        ScreenEngine.getEngine().push(textForm);
    }
}
